﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginFramework.Common;
using QLBH.Category.Presenter;
using System.Threading;
using System.Windows.Forms;
using QLBH.Model.Controller;
using QLBH.StandardUI;
using DevComponents.DotNetBar;
using QLBH.FeatureBase.Modules.Category;
using QLBH.FeatureBase;
using QLBH.Base;
using QLBH.ModelInterface;

namespace QLBH.Category
{
    [ExtensionOf(typeof(QLNganhHangToolExtensionPoint))]
    public class QLNganhHangTool:BaseView,IQLNganhHangTool
    {
        public FormViewBase GetView(object arg)
        {
            return new QLNganhHangTool();
        }
        public QLNganhHangTool()
            :base()
        {

        }
        public override void AddNew(object sender, EventArgs e)
        {
            AddNganhHang addnew = new AddNganhHang();
            if (addnew.ShowDialog()==DialogResult.OK)
            {
                LoadDSNganhHang();
            }
            base.AddNew(sender, e);
        }
        public override void EditItem(object sender, EventArgs e)
        {
            if (DataGridView.SelectedRows.Count>0)
            {
                var model = DataGridView.SelectedRows[0].DataBoundItem as IQLNganhHangModel;

                AddNganhHang editdlg = new AddNganhHang();
                editdlg.DlgType = eDlgType.EDIT;
                editdlg.Model = model;
                if (editdlg.ShowDialog() == DialogResult.OK)
                {
                    LoadDSNganhHang();
                }
            }
            
            base.EditItem(sender, e);
        }
        public override void DeleteItem(object sender, EventArgs e)
        {
            if (DataGridView.SelectedRows.Count > 0)
            {
                var model = DataGridView.SelectedRows[0].DataBoundItem as IQLNganhHangModel;

               try
               {
                   NganhHangController.GetInstance().Delete(model.ToEntity());
                   LoadDSNganhHang();
               }
               catch (System.Exception ex)
               {
                   ShowMessage.Instance.ShowError(ex.InnerException.Message);
               }
            }
            base.DeleteItem(sender, e);
        }
        public override void RefreshData(object sender, System.EventArgs e)
        {
            base.RefreshData(sender, e);
            LoadDSNganhHang();
        }
        public override void BuildSearchMenu()
        {
            ButtonItem btnSearchByID = new ButtonItem("btnSearchByID", "Tìm theo Mã NH");
            ButtonItem btnSearchByName = new ButtonItem("btnSearchByName", "Tìm theo Tên NH");

            BtnSearch.SubItems.Add(btnSearchByID);
            BtnSearch.SubItems.Add(btnSearchByName);

            btnSearchByID.Click += btnSearchByID_Click;

            btnSearchByName.Click += btnSearchByName_Click;
            base.BuildSearchMenu();
        }

        void btnSearchByName_Click(object sender, EventArgs e)
        {
            List<IQLNganhHangModel> source = (List<IQLNganhHangModel>)DataSource;

            source = source.Where(m => m.TenNH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }

        void btnSearchByID_Click(object sender, EventArgs e)
        {
            List<IQLNganhHangModel> source = (List<IQLNganhHangModel>)DataSource;

            source = source.Where(m => m.MaNH.Contains(TbxSearchInput.ControlText)).ToList();

            DataGridView.DataSource = source;
        }
        private void LoadDSNganhHang()
        {
            QLNganhHangPresenter presenter = new QLNganhHangPresenter();
            DataSource = presenter.GetDSNganhHang();
            DataGridView.DataSource = DataSource;

            DataGridView.Columns["ID"].Visible = false;
            DataGridView.Columns["ID"].DisplayIndex = 5;

            DataGridView.Columns["MaNH"].HeaderText = "Mã NH";
            DataGridView.Columns["MaNH"].DisplayIndex = 0;

            DataGridView.Columns["TenNH"].HeaderText = "Tên NH";
            DataGridView.Columns["TenNH"].DisplayIndex = 1;

            DataGridView.Columns["Mota"].HeaderText = "Mô Tả";
            DataGridView.Columns["Mota"].DisplayIndex = 2;

            DataGridView.Columns["GhiChu"].HeaderText = "Ghi Chú";
            DataGridView.Columns["GhiChu"].DisplayIndex = 3;

            DataGridView.Columns["NgayTao"].HeaderText = "Ngày Tạo";
            DataGridView.Columns["NgayTao"].DisplayIndex = 4;

            DataGridView.Columns["NgayTao"].DefaultCellStyle.Format =
                Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern;
        }
        public override void LoadForm(object sender, EventArgs e)
        {
            base.LoadForm(sender, e);
            LoadDSNganhHang();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // QLNganhHangTool
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
            this.Name = "QLNganhHangTool";
            this.Size = new System.Drawing.Size(0, 0);
            this.ResumeLayout(false);

        }
    }
}
